// pages/shopping.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    choose_active: null,
    type_: null
  },
  // 打开购物车购买页
  openTypeFun(e) {
    let type_ = e.currentTarget.dataset.type_
    let data =  e.currentTarget.dataset.item
    let typeArr = []
    // 该属性传入 type 选择组件控制 type item 项选中状态
    let typeState = []
    // 计算最高价和最低价,顺便为 type 状态设置数组，和生成 type 各个状态的选择状态控制数组，最后在一起设置给 typeArr , typeState
    let min_price = 0
    let max_price = 0
    for (let key in data.package) {
      typeArr.push(key)
      typeState.push(false)
      if (min_price == 0 || min_price >= data.package[key]) {
        min_price = data.package[key]
      }
      if (max_price == 0 || max_price <= data.package[key]) {
        max_price = data.package[key]
      }
    }
    let price = {}
    price.min_price = min_price
    price.max_price = max_price
    data.price = price
    this.setData({
      data,
      type_,
      typeArr,
      typeState,
      choose_active: true,
    })
  },
  openDetails(e) {
    console.log(e.currentTarget.dataset.data);
    let data = e.currentTarget.dataset.data
    // 打开详情页
    wx.navigateTo({
      url: '../goodsDetails/goodsDetails?data=' + JSON.stringify(data),
    })
  },
  gotop(e) {
    wx.pageScrollTo({
      scrollTop: 0
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取与用户绑定的购物车数据包 [{ID:'goodsID',type:'typeName',number:'cartNumber'}]
    let cart_datas = [{
      ID: 1,
      type: 'one',
      number: 2
    }]
    let data = [{
      goodsID: 1,
      img: "https://c-ssl.duitang.com/uploads/item/202005/13/20200513204238_igdfd.webp",
      title: "D.va",
      package: {
        one: 999,
        two: 234,
        three: 980
      },
      imgs:[],
      left_num: 3,
    }, {
      goodsID: 1,
      img: "https://c-ssl.duitang.com/uploads/item/202005/13/20200513204238_igdfd.webp",
      title: "D.va",
      package: {
        one: 999,
        two: 234,
        three: 980
      },
      left_num: 3
    }]
    this.setData({
      datas: data,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})